
module ThreadSessionModule
	def initialize
		@sessions = {}
		@counter = 0
	end
	
	def receive_request soc, req
		if req =~ /^301 SESSION CREATE$/ then
			token = make_token.to_s
			
			soc.write "201 SESSION CREATED #{token}\n"
			@sessions[token] = {}
			Thread.current["session"] = @sessions[token]
			Thread.current["session_id"] = token
			
		elsif m = req.match(/^302 SESSION RESTORE (.+)$/) then
			token = m[1]
			
			if @sessions[token] then
				Thread.current["session"] = @sessions[token]
				Thread.current["session_id"] = token
				soc.write "202 SESSION RESTORED #{token}\n"
			else
				soc.write "401 SESSION NOT FOUND\n"
			end
			
		elsif req =~ /^303 SESSION STATUS (.+)$/ then
			if Thread.current["session_id"] then
				soc.write "203 SESSION OPENED AS #{Thread.current["session_id"]}\n"
			else
				soc.write "204 SESSION NOT OPENED\n"
			end
		end
	end
	
	def make_token
		@counter += 1
	end
end
